# -*- coding:utf-8 -*-

# @Time    : 2023/5/13 02:24
# @Author  : zengwenjia
# @Email   : zengwenjia@lingxi.ai
# @File    : user_info_extract.py
# @Software: LLM_internal

# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
from agent.llm_agent import LLMAgent
from db.es.es_service import ElasticsearchStorage
from common import constants
from data_generate import utils

default_template = """
作为一个专业的对话评价工程师,你需要根据语法正确性，语义连贯性，回答准确性，上下文理解，解决用户问题上来评估下面的一通对话，你必须十分严格。
评价标准1.助手的原则是帮助用户解决问题，助手的回复是否清晰明了，必须是用户能够理解的，不能是一些专业术语，不能是一些模棱两可的回答。
评价标准2.查看助手的表述是否正确，完整，合理，是否解决了用户的问题。
评价标准3.如果助手的回答比较笼统，没有具体方案的，扣除大量分数。
评价标准4.助手主要是为了解决用户保险规划和养老规划上的问题，回复其他场景的问题扣除大量分数。
按照1-100给出一个分数,按照下面格式 解释优缺点的时候最好给出例句：
示例开始：
分数：
优点：
缺点：
综合看法：
示例结束
任务：
===
{conversation}
===
        """


class EvaluateConversation(LLMAgent):

    def __init__(self, conversation):
        self.prompt = default_template
        self.prompt = self.prompt.format(conversation=conversation)
        super().__init__(self.prompt)


def format_conversation_history(conversation_context):
    dialogue_history = ""
    # 倒序遍历record_list
    for record in conversation_context[::-1]:
        if record['role'] == constants.ROLE_USER:
            dialogue_history = "用户:" + record["text_info"] + "\n" + dialogue_history
        elif record['role'] == constants.ROLE_ASSISTANT:
            dialogue_history = "助手:" + record["text_info"] + "\n" + dialogue_history
        if len(dialogue_history) > 1000:
            break
    return dialogue_history


def format_conversation_history_content(conversation_context):
    dialogue_history = ""
    # 倒序遍历record_list
    for record in conversation_context[::-1]:
        if record['role'] == constants.ROLE_USER:
            dialogue_history = "用户:" + record["content"] + "\n" + dialogue_history
        elif record['role'] == constants.ROLE_ASSISTANT:
            dialogue_history = "助手:" + record["content"] + "\n" + dialogue_history
        if len(dialogue_history) > 1000:
            break
    return dialogue_history


def evaluate_es_conversation():
    es = ElasticsearchStorage()

    aggregation_query = {
        "size": 0,
        "aggs": {
            "sessions": {
                "terms": {
                    "field": "session_id",
                    "size": 100
                },
                "aggs": {
                    "messages": {
                        "top_hits": {
                            "size": 100,
                            "_source": {
                                "includes": ["role", "text_info", "message_time"]
                            }
                        }
                    }
                }
            }
        }
    }
    # 执行查询
    result = es.search_documents_by_agg("dialogues", aggquery=aggregation_query)
    if result:
        # 提取聚合结果
        aggregated_sessions = []
        for bucket in result:
            session_id = bucket["key"]
            messages = [hit["_source"] for hit in bucket["messages"]["hits"]["hits"]]
            aggregated_sessions.append({"session_id": session_id, "messages": messages})

            sorted_messages = []
            # 输出聚合结果
            for session in aggregated_sessions:
                # 对话排序
                sorted_messages = sorted(session['messages'], key=lambda x: x['message_time'])

            session_message = format_conversation_history(sorted_messages)
            ec = EvaluateConversation(session_message)
            result = ec.chat_with_azure()
            print(f"对话内容：{sorted_messages}")
            print(f"session_id={session_id} 结果是:{result}")
            print("======")


def evaluate_gen_conversation():
    file_path = "../data_set/bot_dialogue/bot_dialogue.json"
    datas = utils.jload(file_path)
    for data in datas:
        session_id = data['id']
        messages = data['messages']
        session_message = format_conversation_history_content(messages)

        ec = EvaluateConversation(session_message)
        result = ec.chat_with_azure()
        print(f"对话内容：{messages}")
        print(f"session_id={session_id} 结果是:{result}")
        print("======")


if __name__ == '__main__':
    evaluate_gen_conversation()
